package com.ww.springboot.boot.thread.study;

import java.util.concurrent.TimeUnit;

/**
 *
 * 6种状态
 *
 * RUNNABLE 运行
 * BLOCKED 阻塞
 * WAITING 等待
 * TIMED_WAITING 等待指定时间
 * TERMINATED 结束
 * NEW 创建
 *
 * @author wanwei
 * @since 2021-04-03 16:31
 */
public class ThreadStatusDemo {

    public static void main(String[] args) throws InterruptedException {
        //new 状态    创建
        newStatus();
        //RUNNABLE   可运行
        runnableStatus();
        //WAITING    线程等待
        waitingStatus();
        //BLOCKED    线程阻塞
        blockedStatus();
        //TERMINATED 线程运行完成
        Thread thread = new Thread(()->{

        });
        thread.start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println(thread.getState());
        //让出资源
        Thread.yield();
    }

    /**
     * 资源等待
     */
    private static void blockedStatus() {
        Thread thread = new Thread(()->{
            System.out.println("1获取锁");
            test();
            System.out.println("1释放锁");
        });
        thread.start();

        Thread thread2 = new Thread(()->{
            System.out.println("2获取锁");
            test();
            System.out.println("2释放锁");
        });
        thread2.start();
        System.out.println(thread.getName() + ":" + thread.getState()+ System.currentTimeMillis());
        System.out.println(thread2.getName() + ":" + thread2.getState()+ System.currentTimeMillis());
        sleep(500);
        System.out.println(thread.getName() + ":" + thread.getState()+ System.currentTimeMillis());
        System.out.println(thread2.getName() + ":" + thread2.getState()+ System.currentTimeMillis());
        sleep(500);
        System.out.println(thread.getName() + ":" + thread.getState()+ System.currentTimeMillis());
        System.out.println(thread2.getName() + ":" + thread2.getState()+ System.currentTimeMillis());
        sleep(500);
        System.out.println(thread.getName() + ":" + thread.getState()+ System.currentTimeMillis());
        System.out.println(thread2.getName() + ":" + thread2.getState()+ System.currentTimeMillis());
    }

    private static synchronized void test(){
        sleep(1000);
    }

    private static void newStatus() {
        Thread thread = new Thread();
        System.out.println(thread.getName() + ":" + thread.getState());
    }

    private static void runnableStatus() {
        Thread thread = new Thread();
        thread.start();
        System.out.println(thread.getName() + ":" + thread.getState());
    }

    /**
     * start :无需等待run方法体代码执行完毕，可以直接继续执行下面的代码
     * run :常规调用并没有开启多线程
     */
    private static void waitingStatus() {
        Thread thread = new Thread(() -> {
            sleep(1000);
            System.out.println("线程1" + System.currentTimeMillis());
        });
        thread.start();
        Thread thread2 = new Thread(() -> {
            sleep(1000);
            System.out.println("线程2" + System.currentTimeMillis());
        });
        thread2.start();
        System.out.println(thread.getName() + ":" + thread.getState()+ System.currentTimeMillis());
        System.out.println(thread2.getName() + ":" + thread2.getState()+ System.currentTimeMillis());
    }

    private static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
